from collections import OrderedDict

from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView, ListCreateAPIView
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination
from rest_framework.views import APIView

from apps.meiduo_admin.serializers.user import UserModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.users.models import User
from rest_framework.response import Response


# 列表视图  获取所有数据 和 添加一条数据
# 详情  修改和删除一条数据  获取一条数据


# http://127.0.0.1:8000/meiduo_admin/users/?page=3&&pagesize=1
class UserListView(ListCreateAPIView):
    # 1指定查询集
    # queryset = User.objects.all()
    # 2指定序列化器
    serializer_class = UserModelSerializer

    # 添加分页类
    pagination_class = PageNum

    # - 1查询 所有用户
    # - 2 把数据转为字典 （序列化器）
    # - 3 返回数据

    def get_queryset(self):


        # 返回查询集
        # 获取前端 传来的 keyword参数

        keyword = self.request.query_params.get('keyword')
        # 判断是否有搜索内容
        if keyword:
            return User.objects.filter(username__contains=keyword)

        return User.objects.all()
